package cn.jjxx.modules.activiti.listener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.TaskListener;

import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.core.utils.SpringContextHolder;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.activiti.controller.ActivitiFlowService;
import cn.jjxx.modules.activiti.workflow.BillBean;
import cn.jjxx.modules.activiti.workflow.Config;
import cn.jjxx.modules.sys.entity.User;

public class SetAssigneeByVariableListener implements TaskListener{

	private static final long serialVersionUID = -2804666097994872566L;
	
	private Expression assigneeVariable;//参数人员表达式设置
	
	private Expression paramFeilds;//参数字段，多个用英文逗号隔开
	
	private Expression functionName;//前端获取用户名的函数
	
	private static final String JS = "JavaScript";//脚本
	
	private static String funcName = "getUsername";
	
	@Override
	public void notify(DelegateTask delegateTask) {
		if(functionName!=null){
			funcName = String.valueOf(functionName.getValue(delegateTask.getExecution()));
		}
		if(assigneeVariable!=null&&paramFeilds!=null){
			String username = null;
			Map<String,Object> variables = delegateTask.getVariables();
			BillBean billBean = (BillBean) variables.get(Config.variable.表单信息.toString());
			String feilds = String.valueOf(paramFeilds.getValue(delegateTask.getExecution()));
			String expression = String.valueOf(assigneeVariable.getValue(delegateTask.getExecution()));
			List<String> feildsList = Arrays.asList(feilds.split(","));
			Map<String,Object> billInfo = SpringContextHolder.getBean(ActivitiFlowService.class).getBillBeanInfo(billBean); 
			if(billInfo.size()>0){
				for(String feild:feildsList){
					if(expression.contains("${"+feild+"}")&&billInfo.get(feild)!=null){
						expression = expression.replace("${"+feild+"}", String.valueOf(billInfo.get(feild)));
					}
				}
				username = getUsername(expression, funcName);
			}
			if(StringUtils.isNotEmpty(username)){
				User user = SpringContextHolder.getBean(ActivitiFlowService.class).getUser(username); 
				if(!ObjectUtils.isNullOrEmpty(user)){
					delegateTask.setAssignee(user.getId());
				}
			}
		}
	}
	
	/**
	 * @Description: 获取配置的用户 .<br>
	 * @param expression 表达式.<br>
	 * @param functionName JS的function的名称.<br>
	 * @author 郑成功 .<br>
	 * @date 2018-11-22 上午10:04:02.<br>
	 */
	private String getUsername(String expression,String functionName){
		ScriptEngineManager m = new ScriptEngineManager();
	    //获取JavaScript执行引擎
	    ScriptEngine engine = m.getEngineByName(JS);
	    //执行JavaScript代码
	    try {
	    	engine.eval(expression);     
	    	Invocable inv = (Invocable) engine;     
	    	String username = String.valueOf(inv.invokeFunction(functionName));  
			return username;
		} catch (Exception e) {
			System.out.println(SetAssigneeByVariableListener.class.getName()+".getUsername():"+e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
}
